﻿using System;
using System.IO;
using System.Text;

namespace WikiUploader.Classes
{
   /// <summary>
   /// Redirects console output to the Line written event.
   /// </summary>
   public class ConsoleRedirection : TextWriter
   {
      /// <summary>
      /// Occurs when a line is written.
      /// </summary>
      public event LineEventHandler LineWritten;

      /// <summary>
      /// Event Handler for line written event.
      /// </summary>
      /// <param name="sender">Sender of event.</param>
      /// <param name="args">LineEventArgs</param>
      public delegate void LineEventHandler (object sender, LineEventArgs args);

      /// <summary>
      /// Writes a string followed by a line terminator to the text stream.
      /// </summary>
      /// <param name="value">The string to write. If <paramref name="value"/> is null, only the line termination characters are written. </param>
      /// <exception cref="T:System.ObjectDisposedException">The <see cref="T:System.IO.TextWriter"/> is closed. </exception>
      /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
      /// <filterpriority>1</filterpriority>
      public override void WriteLine (string value)
      {
         base.WriteLine (value);
         if (LineWritten != null)
         {
            LineWritten (this, new LineEventArgs (value));
            LineWritten (this, new LineEventArgs (Environment.NewLine));
         }
      }

      /// <summary>
      /// Writes a line terminator to the text stream.
      /// </summary>
      /// <exception cref="T:System.ObjectDisposedException">The <see cref="T:System.IO.TextWriter"/> is closed. </exception>
      /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
      /// <filterpriority>1</filterpriority>
      public override void WriteLine ()
      {
         base.WriteLine ();
         if (LineWritten != null)
            LineWritten (this, new LineEventArgs (Environment.NewLine));
      }

      /// <summary>
      /// When overridden in a derived class, returns the <see cref="T:System.Text.Encoding"/> in which the output is written.
      /// </summary>
      /// <returns>
      /// The Encoding in which the output is written.
      /// </returns>
      /// <filterpriority>1</filterpriority>
      public override Encoding Encoding
      {
         get { return Encoding.UTF8; }
      }
   }

   /// <summary>
   /// Event arguments for when a line is written.
   /// </summary>
   public class LineEventArgs : EventArgs
   {
      /// <summary>
      /// Initializes a new line event args object.
      /// </summary>
      /// <param name="line">Line beging written.</param>
      public LineEventArgs (string line)
      {
         this.Line = line;
      }

      /// <summary>
      /// Gets or Sets the character written.
      /// </summary>
      public string Line { get; private set; }
   }
}
